package controllers

import (
	"encoding/json"
	"strconv"
	"supershare/backend/middleware"
	"supershare/backend/models"

	"github.com/beego/beego/v2/server/web"
)

// RechargePackageController 充值套餐控制器
type RechargePackageController struct {
	web.Controller
}

// List 获取充值套餐列表
func (c *RechargePackageController) List() {
	// 获取当前管理员信息
	admin, err := middleware.GetCurrentAdmin(c.Ctx)
	if err != nil || admin == nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "未授权访问",
		}
		c.ServeJSON()
		return
	}

	// 获取分页参数
	page, _ := c.GetInt("page", 1)
	pageSize, _ := c.GetInt("pageSize", 10)

	// 获取过滤参数
	name := c.GetString("name")
	status, _ := c.GetInt8("status", -1)
	isHot, _ := c.GetInt8("is_hot", -1)

	// 构建过滤条件
	filters := make(map[string]interface{})
	if name != "" {
		filters["name"] = name
	}
	if status != -1 {
		filters["status"] = status
	}
	if isHot != -1 {
		filters["is_hot"] = isHot
	}

	// 添加加盟商过滤
	if admin.FranchiseId > 0 {
		filters["franchise_id"] = admin.FranchiseId
	}

	// 获取充值套餐列表
	packs, total, err := models.GetRechargePackageList(page, pageSize, filters)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "获取充值套餐列表失败: " + err.Error(),
		}
		c.ServeJSON()
		return
	}

	// 返回结果
	c.Data["json"] = map[string]interface{}{
		"success": true,
		"data": map[string]interface{}{
			"list":  packs,
			"total": total,
		},
	}
	c.ServeJSON()
}

// GetOne 获取单个充值套餐详情
func (c *RechargePackageController) GetOne() {
	// 获取当前管理员信息
	admin, err := middleware.GetCurrentAdmin(c.Ctx)
	if err != nil || admin == nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "未授权访问",
		}
		c.ServeJSON()
		return
	}

	// 获取ID参数
	idStr := c.Ctx.Input.Param(":id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "无效的ID参数",
		}
		c.ServeJSON()
		return
	}

	// 获取充值套餐详情，带加盟商权限验证
	pack, err := models.GetRechargePackageByIdWithFranchise(id, admin.FranchiseId)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "获取充值套餐详情失败: " + err.Error(),
		}
		c.ServeJSON()
		return
	}

	// 返回结果
	c.Data["json"] = map[string]interface{}{
		"success": true,
		"data":    pack,
	}
	c.ServeJSON()
}

// Create 创建充值套餐
func (c *RechargePackageController) Create() {

	admin, _ := middleware.GetCurrentAdmin(c.Ctx)
	if admin == nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "未授权访问",
		}
		c.ServeJSON()
		return
	}

	// 解析请求数据
	var pack models.RechargePackage
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &pack)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "无效的请求数据: " + err.Error(),
		}
		c.ServeJSON()
		return
	}
	pack.CreatedBy = admin.Username
	pack.UpdatedBy = admin.Username
	pack.BeforeInsert()
	pack.FranchiseId = admin.FranchiseId
	// 创建充值套餐
	id, err := models.CreateRechargePackage(&pack)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "创建充值套餐失败: " + err.Error(),
		}
		c.ServeJSON()
		return
	}

	// 返回结果
	c.Data["json"] = map[string]interface{}{
		"success": true,
		"message": "创建充值套餐成功",
		"data": map[string]interface{}{
			"id": id,
		},
	}
	c.ServeJSON()
}

// Update 更新充值套餐
func (c *RechargePackageController) Update() {
	admin, _ := middleware.GetCurrentAdmin(c.Ctx)
	if admin == nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "未授权访问",
		}
		c.ServeJSON()
		return
	}
	// 获取ID参数
	idStr := c.Ctx.Input.Param(":id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "无效的ID参数",
		}
		c.ServeJSON()
		return
	}
	// 解析请求数据
	var pack models.RechargePackage
	err = json.Unmarshal(c.Ctx.Input.RequestBody, &pack)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "无效的请求数据: " + err.Error(),
		}
		c.ServeJSON()
		return
	}

	if admin.FranchiseId > 0 && admin.FranchiseId != pack.FranchiseId {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "没有权限访问",
		}
		c.ServeJSON()
		return
	}
	// 设置ID
	pack.Id = id

	// 更新充值套餐
	err = models.UpdateRechargePackage(&pack)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "更新充值套餐失败: " + err.Error(),
		}
		c.ServeJSON()
		return
	}

	// 返回结果
	c.Data["json"] = map[string]interface{}{
		"success": true,
		"message": "更新充值套餐成功",
	}
	c.ServeJSON()
}

// Delete 删除充值套餐
func (c *RechargePackageController) Delete() {
	admin, err := middleware.GetCurrentAdmin(c.Ctx)
	if err != nil || admin == nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "未授权访问",
		}
		c.ServeJSON()
		return
	}

	// 获取ID参数
	idStr := c.Ctx.Input.Param(":id")
	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "无效的ID参数",
		}
		c.ServeJSON()
		return
	}

	// 获取充值套餐详情，带加盟商权限验证
	_, err = models.GetRechargePackageByIdWithFranchise(id, admin.FranchiseId)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "获取充值套餐详情失败: " + err.Error(),
		}
		c.ServeJSON()
		return
	}

	// 删除充值套餐
	err = models.DeleteRechargePackage(id)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "删除充值套餐失败: " + err.Error(),
		}
		c.ServeJSON()
		return
	}

	// 返回结果
	c.Data["json"] = map[string]interface{}{
		"success": true,
		"message": "删除充值套餐成功",
	}
	c.ServeJSON()
}

// GetActive 获取激活的充值套餐列表
func (c *RechargePackageController) GetActive() {
	// 获取当前管理员信息
	admin, err := middleware.GetCurrentAdmin(c.Ctx)
	if err != nil || admin == nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "未授权访问",
		}
		c.ServeJSON()
		return
	}

	// 构建过滤条件，包括激活状态和加盟商ID
	filters := make(map[string]interface{})
	filters["status"] = 1 // 激活状态

	// 添加加盟商过滤
	if admin.FranchiseId > 0 {
		filters["franchise_id"] = admin.FranchiseId
	}

	// 获取套餐列表，100是足够大的数量，确保获取所有活跃套餐
	packs, _, err := models.GetRechargePackageList(1, 100, filters)
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"success": false,
			"message": "获取充值套餐列表失败: " + err.Error(),
		}
		c.ServeJSON()
		return
	}

	// 返回结果
	c.Data["json"] = map[string]interface{}{
		"success": true,
		"data":    packs,
	}
	c.ServeJSON()
}
